<?php
/**
 * @分销
 * @功能说明:
 * @更新说明: 暂无更新
 * @文件名: InlevelController.class.php
 * @编码: UTF-8
 * @创建时间: 2016-05-06 16:51
 * @创建人: WENCUN
 */

namespace wap\Controller;


use Think\Controller;

class InlevelController extends Controller
{
    /*
     * 分销商城_佣金收益
     * @date: 2016-05-23 10:42:00
     * @editor: WENCUN
     * */
    public function salerProfit(){
        $profit = array(
            '1' => array(
                '150','500','200','1'//店长(588) 1嗨币
            ),
            '2' => array(
                '160','600','300','1'//经理(1588) 1嗨币
            ),
            '3' => array(
                '160','600','300','1'//总监 1嗨币
            )
        );
        //间推收益
        $indirectProfit = array(
            '30','50'
        );
        //总监有上级总监收益
        $profitZj = array(
            '130','150'
        );
        //总监上级总监收益
        $superiorProfit = array(
            '20','30'
        );
        //流水备注
        $bottlerRemark = array(
            '推荐用户升级嗨呗店长 提成收益','推荐直升嗨呗经理 提成收益','直属下级升级嗨呗经理 提成收益'
        );
        $jlRemark = array(
            '间接推荐店长 提成收益','间接推荐经理 提成收益'
        );
        $njlRemark = array(
            '推荐店长 提成收益','推荐经理 提成收益'
        );
        $zjRemark = array(
            '团队新进店长 提成收益','团队新进经理 提成收益'
        );
        $superiorRemark = array(
            '下级总监团队新进店长 提成收益','下级总监团队新进经理 提成收益'
        );
        $getProFits = M('CustInlevel')
            ->where('cash_at = 0 AND biz_cust_salelevel > 0')->select();
        $profitData = array();
        if($getProFits){
            foreach($getProFits as $key=>$value){
                $mainProfit = M('CustBottler');
                $bottlerData = array(
                    'finance_money' => $profit[$value['biz_cust_salelevel']][$value['type']],
                    'cust_id' => $value['biz_cust_id'],
                    'money_remark' => $bottlerRemark[$value['type']],
                    'is_waste' => '0',
                    'add_at' => time()
                );
                //@todo 收入明细
                $profitData[] = array(
                    'cust_id' => $value['cust_id'],
                    'profit_cust_id' => $value['biz_cust_id'],
                    'order_id' => $value['order_id'],
                    'profit_money' => $profit[$value['biz_cust_salelevel']][$value['type']],
                    'profit_remark' => $bottlerRemark[$value['type']],
                );
                $putProfit = $mainProfit->lock(true)->add($bottlerData);
                if($putProfit){
                    //更新结算时间
                    $levelMain = M('CustInlevel');
                    $signSuccess = $levelMain->lock(true)
                                 ->where(array('id'=>$value['id']))->setField(array('cash_at'=>time()));
                    if($signSuccess){
                        //如果升级类型为(店长升级经理)则没有嗨币
                        if($value['type'] != '2'){
                            //更新嗨币
                            $custEntity = M('CustEntity');
                            $setCustMsg = $custEntity->lock(true)
                                        ->where(array('cust_id'=>$value['biz_cust_id']))
                                        ->setInc('hp_money',intval($profit[$value['biz_cust_salelevel']][3]));
                            if($setCustMsg){
                                //如果上级为经理且自己不是总监
                                $jlId = self::findJl($value['biz_cust_id']);
                                if($jlId){
                                    //经理间推待遇
                                    $jlProfit = array(
                                        'finance_money' => $indirectProfit[$value['type']],
                                        'cust_id' => $jlId,
                                        'money_remark' => $jlRemark[$value['type']],
                                        'is_waste' => '0',
                                        'add_at' => time()
                                    );
                                    //经理收入明细
                                    $profitData[] = array(
                                        'cust_id' => $value['cust_id'],
                                        'profit_cust_id' => $jlId,
                                        'order_id' => $value['order_id'],
                                        'profit_money' => $indirectProfit[$value['type']],
                                        'profit_remark' => $jlRemark[$value['type']],
                                    );
                                    $mainProfit->lock(true)->add($jlProfit);
                                }else{
                                    //如果直推人是经理则拿上级经理的间推奖金
                                    if($value['biz_cust_salelevel'] == '2'){
                                        $njlProfit = array(
                                            'finance_money' => $indirectProfit[$value['type']],
                                            'cust_id' => $value['biz_cust_id'],
                                            'money_remark' => $njlRemark[$value['type']],
                                            'is_waste' => '0',
                                            'add_at' => time()
                                        );
                                        //收入明细
                                        $profitData[] = array(
                                            'cust_id' => $value['cust_id'],
                                            'profit_cust_id' => $value['biz_cust_id'],
                                            'order_id' => $value['order_id'],
                                            'profit_money' => $indirectProfit[$value['type']],
                                            'profit_remark' => $njlRemark[$value['type']],
                                        );
                                        $mainProfit->lock(true)->add($njlProfit);
                                    }
                                }
                                //寻找总监
                                $zjId = self::findZj($value['biz_cust_id']);
                                if($zjId){
                                    //总监待遇
                                    //寻找总监上级总监(如果有上级总监则奖励拆分)
                                    $findZJ = self::zjFindZj($zjId);
                                    if($findZJ){
                                        //只有经理升级的总监才能享受下级总监团队的新增奖励
                                        $zjType = M('CustEntity')->getFieldByCustId($findZJ,'zj_type');
                                        if($zjType == '2'){
                                            //上级总监收益
                                            $zjProfit = array(
                                                'finance_money' => $profitZj[$value['type']],
                                                'cust_id' => $zjId,
                                                'money_remark' => $zjRemark[$value['type']],
                                                'is_waste' => '0',
                                                'add_at' => time()
                                            );
                                            //上级总监上级总监收益
                                            $superiorZj = array(
                                                'finance_money' => $superiorProfit[$value['type']],
                                                'cust_id' => $findZJ,
                                                'money_remark' => $superiorRemark[$value['type']],
                                                'is_waste' => '0',
                                                'add_at' => time()
                                            );
                                            //总监收入明细
                                            $profitData[] = array(
                                                'cust_id' => $value['cust_id'],
                                                'profit_cust_id' => $zjId,
                                                'order_id' => $value['order_id'],
                                                'profit_money' => $profitZj[$value['type']],
                                                'profit_remark' => $zjRemark[$value['type']],
                                            );
                                            $profitData[] = array(
                                                'cust_id' => $value['cust_id'],
                                                'profit_cust_id' => $findZJ,
                                                'order_id' => $value['order_id'],
                                                'profit_money' => $superiorProfit[$value['type']],
                                                'profit_remark' => $superiorRemark[$value['type']],
                                            );
                                            $zjTcheng = $mainProfit->lock(true)->add($zjProfit);
                                            $superiorTcheng = $mainProfit->lock(true)->add($superiorZj);
                                            if($zjTcheng && $superiorTcheng){
                                                $mainProfit->commit();
                                                $levelMain->commit();
                                                $custEntity->commit();
                                                //调取收入明细函数
                                                inProfit($profitData);
                                                curl($_SERVER['SERVER_NAME'].'/inlevel/inlevelZj');
                                            }else{
                                                $mainProfit->rollback();
                                                $levelMain->rollback();
                                                $custEntity->rollback();
                                            }
                                        }else{
                                            //如果上级总监不是经理升级总监类型则没有下级总监团队新增奖金
                                            $zjProfit = array(
                                                'finance_money' => $profitZj[$value['type']]+$superiorProfit[$value['type']],
                                                'cust_id' => $zjId,
                                                'money_remark' => $zjRemark[$value['type']],
                                                'is_waste' => '0',
                                                'add_at' => time()
                                            );
                                            //总监收入明细
                                            $profitData[] = array(
                                                'cust_id' => $value['cust_id'],
                                                'profit_cust_id' => $zjId,
                                                'order_id' => $value['order_id'],
                                                'profit_money' => $profitZj[$value['type']]+$superiorProfit[$value['type']],
                                                'profit_remark' => $zjRemark[$value['type']],
                                            );
                                            $zjTcheng = $mainProfit->lock(true)->add($zjProfit);
                                            if($zjTcheng){
                                                $mainProfit->commit();
                                                $levelMain->commit();
                                                $custEntity->commit();
                                                //调取收入明细函数
                                                inProfit($profitData);
                                                curl($_SERVER['SERVER_NAME'].'/inlevel/inlevelZj');
                                            }else{
                                                $mainProfit->rollback();
                                                $levelMain->rollback();
                                                $custEntity->rollback();
                                            }
                                        }
                                    }else{
                                        //如果上级总监没有上级总监
                                        $zjProfit = array(
                                            'finance_money' => $profitZj[$value['type']]+$superiorProfit[$value['type']],
                                            'cust_id' => $zjId,
                                            'money_remark' => $zjRemark[$value['type']],
                                            'is_waste' => '0',
                                            'add_at' => time()
                                        );
                                        //总监收入明细
                                        $profitData[] = array(
                                            'cust_id' => $value['cust_id'],
                                            'profit_cust_id' => $zjId,
                                            'order_id' => $value['order_id'],
                                            'profit_money' => $profitZj[$value['type']]+$superiorProfit[$value['type']],
                                            'profit_remark' => $zjRemark[$value['type']],
                                        );
                                        $zjTcheng = $mainProfit->lock(true)->add($zjProfit);
                                        if($zjTcheng){
                                            $mainProfit->commit();
                                            $levelMain->commit();
                                            $custEntity->commit();
                                            //调取收入明细函数
                                            inProfit($profitData);
                                            curl($_SERVER['SERVER_NAME'].'/inlevel/inlevelZj');
                                        }else{
                                            $mainProfit->rollback();
                                            $levelMain->rollback();
                                            $custEntity->rollback();
                                        }
                                    }
                                }else{
                                    $mainProfit->commit();
                                    $levelMain->commit();
                                    $custEntity->commit();
                                    //调取收入明细函数
                                    inProfit($profitData);
                                    curl($_SERVER['SERVER_NAME'].'/inlevel/inlevelZj');
                                }
                            }else{
                                $mainProfit->rollback();
                                $levelMain->rollback();
                                $custEntity->rollback();
                            }
                        }else{
                            //调取收入明细函数
                            inProfit($profitData);
                        }
                    }else{
                        $mainProfit->rollback();
                        $levelMain->rollback();
                    }
                }else{
                    $mainProfit->rollback();
                }
            }
        }
    }

    /*
    * 分销商城_我要找总监
    * @date: 2016-3-25 下午6:37:56
    * @editor: XING
    */
    public function findZj($uid){
        $getSalerData = M('CustEntity')
            ->field('salelevel,parent_id')->where(array('cust_id'=>$uid))->find();
        if($getSalerData['salelevel'] == '3'){
            return $uid;
        }else if(!$getSalerData['parent_id']){
            return 0;
        }else{
            return self::findZj($getSalerData['parent_id']);
        }
    }

    /*
     * 分销商城_寻找总监上级总监
     * @date: 2016-05-23 11:34:00
     * @editor: WENCUN
     * */
    public function zjFindZj($uid,$isZj = true){
        $getSalertData = M('CustEntity')
                       ->field(array('salelevel','parent_id'))->where(array('cust_id'=>$uid))->find();
        if($getSalertData['salelevel'] == '3'){
            if($isZj == false){
                return $uid;
            }
        }
        if($getSalertData['parent_id']){
            return self::zjFindZj($getSalertData['parent_id'],false);
        }else{
            return 0;
        }
    }

    /*
     * 分销商城_找上级经理
     * @date: 2016-05-23 10:12:00
     * @editor: WENCUN
     * */
    public function findJl($uid){
        $getSalerData = M('CustEntity')
            ->field(array('salelevel','parent_id'))->where(array('cust_id'=>$uid))->find();
        if($getSalerData['salelevel'] == '3'){
            return 0;
        }else if(!$getSalerData['parent_id']){
            return 0;
        }else{
            $findJl = M('CustEntity')
                    ->field(array('salelevel'))->where(array('cust_id'=>$getSalerData['parent_id']))->find();
            if($findJl['salelevel'] == '2'){
                return $getSalerData['parent_id'];
            }else{
                return 0;
            }
        }
    }

    /*
     * 分销商城_经理升级总监(店长->总监[嗨币至少80个],经理->总监[嗨币至少60个])
     * @date: 2016-05-23 14:59:00
     * @editor: WENCUN
     * */
    public function inlevelZj(){
        $getCustMsg = M('CustEntity')->field(array('cust_id','salelevel','hp_money'))->where('salelevel > 0 AND salelevel != 3')->select();
        foreach($getCustMsg as $value){
            if($value['salelevel'] == '1'){
                if($value['hp_money'] >= 80){
                    M('CustEntity')->where(array('cust_id'=>$value['cust_id']))->setField(array('salelevel'=>'3','zj_type'=>'1'));
                    //echo "恭喜嗨呗店长 ".$value['cust_id']." 升级为嗨呗总监<br/>";
                    //总监微信消息模板推送
                    curl($_SERVER['SERVER_NAME'].'/jsssdk/inlevelZj',array(
                        'cust_id' => $value['cust_id']
                    ));
                }
            }
            if($value['salelevel'] == '2'){
                if($value['hp_money'] >= 60){
                    M('CustEntity')->where(array('cust_id'=>$value['cust_id']))->setField(array('salelevel'=>'3','zj_type'=>'2'));
                    //echo "恭喜嗨呗经理 ".$value['cust_id']." 升级为嗨呗总监<br/>";
                    //总监微信消息模板推送
                    curl($_SERVER['SERVER_NAME'].'/jsssdk/inlevelZj',array(
                        'cust_id' => $value['cust_id']
                    ));
                }
            }
        }
    }

//    /*
//     * 分销商城_1588升级总监(团队人数>80 团队1588成员人数>40)
//     * @date: 2016-05-06 16:52:00
//     * @editor: WENCUN
//     * */
//    public function inlevelZj(){
//        $getJl = M('CustEntity')->field(array('cust_id'))->where(array('salelevel'=>'2','check'=>'1'))->select();
//        foreach($getJl as $value){
//            $groupSum = self::groupSum($value['cust_id']);
//            $groupJl = self::groupJl($value['cust_id']);
//            //echo $value['cust_id'].' -- 团队人数:'.$groupSum.' -- 团队1588人数:'.$groupJl.'<br/>';
//            if($groupSum>79 && $groupJl>=40){
//                //如果满足升级条件则升级该用户的等级为总监
//                M('CustEntity')->where(array('cust_id'=>$value['cust_id']))->setField(array('salelevel'=>'3','update_at'=>time()));
//            }
//        }
//    }
//
//    /*
//     * 分销商城_团队总人数
//     * @date: 2016-05-06 17:20:00
//     * @editor: WENCUN
//     * */
//    static function groupSum($id,$sum=0){
//        $xj = M('CustEntity')->field(array('cust_id'))->where(array('parent_id'=>$id))->select();
//        if($xj){
//            foreach ($xj as $key => $value) {
//                if($sum<81) {
//                    $sum = self::groupSum($value['cust_id'], $sum + 1);
//                }
//            }
//            return $sum;exit;
//        }else{
//            return $sum;exit;
//        }
//    }
//
//    /*
//     * 分销商城_团队1588人数
//     * @date: 2016-05-07 10:14:00
//     * @editor: WENCUN
//     * */
//    static function groupJl($id,$sum=0){
//        $xj = M('CustEntity')->field(array('cust_id','salelevel'))->where(array('parent_id'=>$id))->select();
//        if($xj){
//            foreach($xj as $key=>$value){
//                if($value['salelevel'] == '2'){
//                    $sum = $sum+1;
//                }
//                $sum = self::groupJl($value['cust_id'],$sum);
//            }
//            return $sum;exit;
//        }else{
//            return $sum;exit;
//        }
//    }

}